package com.example.hemin.fnb.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hemin.fnb.R;
import com.example.hemin.fnb.ui.activity.OrderDetails;
import com.example.hemin.fnb.ui.adapter.OrderFromAdapter;
import com.example.hemin.fnb.ui.base.BaseMvpFragment;
import com.example.hemin.fnb.ui.bean.OrderFromBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class OrderFromFragment extends BaseMvpFragment {


    Unbinder unbinder;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.image1)
    ImageView image1;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private List<OrderFromBean> beans = new ArrayList<>();
    private OrderFromAdapter adapter = new OrderFromAdapter(R.layout.order_from_adapter, beans);

    public static OrderFromFragment getInstance(int status) {
        OrderFromFragment fromFragment = new OrderFromFragment();
        Bundle bundle = new Bundle();
        if (status == 0) {
            bundle.putInt("index", 0);
        } else if (status == 1) {
            bundle.putInt("index", 1);
        } else if (status == 2) {
            bundle.putInt("index", 2);
        } else if (status == 3) {
            bundle.putInt("index", 3);
        } else if (status == 4) {
            bundle.putInt("index", 4);
        }
        fromFragment.setArguments(bundle);
        return fromFragment;
    }

    @Override
    public void lazyInitView(View view) {
        int status = this.getArguments().getInt("index");
        if (status == 0) {
            initData();
        } else if (status == 1) {
            initData();
        } else if (status == 2) {
            initData();
        } else if (status == 3) {
            initData();
        } else if (status == 4) {
            initData();
        }
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
                refreshLayout.finishRefresh(100);
                Toast.makeText(getActivity(), "刷新完成", Toast.LENGTH_SHORT).show();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                initData();
                refreshLayout.finishLoadMore(100);
                Toast.makeText(getActivity(), "加载完成", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {
        for (int i = 0; i < 3; i++) {
            OrderFromBean bean = new OrderFromBean("作曲家", "等待收货", "￥3000.20", "x1", "到付", "￥3000.32", 1);
            OrderFromBean bean2 = new OrderFromBean("绘画家", "交易关闭", "￥3000.20", "x1", "包邮", "￥3000.32", 0);
            beans.add(bean);
            beans.add(bean2);
        }
        if (beans.size() > 0) {
            recyclerview.setVisibility(View.VISIBLE);
            text1.setVisibility(View.GONE);
            image1.setVisibility(View.GONE);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerview.setLayoutManager(linearLayoutManager);
             adapter = new OrderFromAdapter(R.layout.order_from_adapter, beans);
            recyclerview.setAdapter(adapter);
        } else if (beans.size() < 1) {
            recyclerview.setVisibility(View.GONE);
            text1.setVisibility(View.VISIBLE);
            image1.setVisibility(View.VISIBLE);
        }
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), OrderDetails.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.order_from_fragment;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
